<?php

namespace app\user\controller;

use app\common\controller\Common;
use app\common\model\Category;
use app\common\model\ReferralCode;
use app\common\model\Site;
use app\common\model\Users;
use think\Db;
use think\facade\App;
use think\Log;
use wx\Wechat;
use wx\Wxapp;

class Index extends Common
{
    public function index()
    {
        $retype=request()->param('retype');
        $openid = request()->param('openid');
        $code = request()->param('code');
        if (is_null($code) || $code == "") {
            if (is_null($openid) || $openid == "") return $this->errJson('参数错误',['flag'=>0]);
            $user = new Users();
            $data_user = $user->where('openid', '=', $openid)->find();
            if (empty($data_user)) return $this->errJson('数据异常',['flag'=>0]);
            //判断是否注册
            $reisMem = $this->isRegist($openid);
            if (false == $reisMem) return $this->errJson('您未注册青檬会员');

            return $this->okJson('青檬欢迎您');//已注册

        } else if (!is_null($code) || $code != "") {//第一次进来
            if ($retype=='wxapp'){
                $wxapp=new Wxapp();
                $reuser_info =$wxapp->getUser_info($code);
            }else{
                $wx = new Wechat();
                $reuser_info = $wx->getUser_info($code,false);
            }
            if ($reuser_info['flag'] == 0) return $this->errJson($reuser_info['msg'],['flag'=>$reuser_info['flag']]);//获取用户信息失败
            $user_info = $reuser_info['data'];
            $user_openid = $user_info['openid'];
            \think\facade\Log::write($reuser_info);
            // 判断用户是否存在
            $User = Users::where('openid', '=', $user_openid)->find();
            if (!empty($User)) {
                //判断是否注册
                $reisMem = $this->isRegist($user_openid);
                if (false == $reisMem) {
                    $User->update_time = time();
                    $User->save();
                    return $this->errJson('您未注册青檬会员', ['openid' => $user_openid]);
                }
                return $this->okJson('青檬欢迎您', ['openid' => $user_openid]);

            } elseif (empty($User)) {
                $udatas = [
                    'member' => -1,
                    'username' => '青檬会员',
                    'openid' => $user_openid,
                    'update_time' => time(),
                    'ctime' => time(),
                ];
                $user = new Users();
                $rets = $user->create($udatas);
                if ($rets) {
                    return $this->errJson('您未注册青檬会员', ['openid' => $user_openid]);
                }
            }
        }
    }

    //服务分类信息
    public function getcategory()
    {
        $cate = Category::field('cid,title,ico,sort')->where('status', '=', 2)->all();
        if ($cate) {
            if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
                $scheme = 'https://';
            } else {
                $scheme = 'http://';
            }
            $host = $scheme . app()->request->host();
            foreach ($cate as $key => $value) {
                $cate[$key]['ico'] = $host . $value['ico'];
            }

            return $this->okJson('ok', $cate);
        }
        return $this->okJson('暂无数据');
    }

    public function area()
    {
        $area = Db::query("select area_id,area_title,citycode FROM area WHERE citycode=471");
        if (count($area) == 0) return $this->errJson('数据异常');
        return $this->okJson('ok', $area);
    }

    //banner
    public function getbanner()
    {
        $Site = Site::field('s_banner')->where('s_id', '=', 1)->find();
        if ($Site) {
            $Site = explode(',', $Site->s_banner);
            if (!empty($Site)) {
                if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
                    $scheme = 'https://';
                } else {
                    $scheme = 'http://';
                }
                $Imgs = [];
                $host = $scheme . app()->request->host();
                foreach ($Site as $key => $value) {
                    $Imgs[$key] = $host . $value;
                }
            }
            return $this->okJson('ok', $Imgs);
        }
        return $this->errJson('暂无数据');
    }

    private function isRegist($openid)
    {//判断有无注册会员
        $retMe = \app\common\model\Member::where('openid', '=', $openid)->find();
        if (empty($retMe)) {
            return false;
        }
        return true;
    }

    public function getMemberType()
    {//
        $openid = request()->param('openid');
        if (is_null($openid) || $openid == "") return $this->errJson('参数错误');
        $reMem = \app\common\model\Member::field('status')->where('openid', '=', $openid)->find();
        if (empty($reMem)) return $this->errJson('数据异常');
        return $this->okJson('会员类型', $reMem);
    }


}
